package com.jiang.controller;

import com.jiang.dao.userimpl;
import com.jiang.entity.User;
import com.jiang.service.Administratorimpl;
import com.jiang.utlis.Base64Utils;
import com.jiang.utlis.Constants;
import com.jiang.utlis.StrUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.jiang.controller
 * @Description:
 * @Date 2022/5/22 14:32
 */
@WebServlet("/user")
public class loginuser extends BaseServlet {

    //    查看用户是否存在
    public String check(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        if (username == null) {
            return Constants.HAS_USER;
        }
        Administratorimpl user = new Administratorimpl(new userimpl());
        boolean b = user.selectOne(username);
        if (b) {
            return Constants.HAS_USER;
        } else {
            return Constants.NOT_HAS_USER;
        }
    }
    //会员登录
    public String login(HttpServletRequest request, HttpServletResponse response) {
        //获取前端的值
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String logincode = request.getParameter("code");
        String auto = request.getParameter("auto");
        //获取域中验证码
        String sessioncode = (String) request.getSession().getAttribute("code");
        //判断用户输入不为空

        if (!StrUtils.empty(username) || !StrUtils.empty(password) || !StrUtils.empty(logincode)) {
            request.getSession().setAttribute("msg", "用户名或密码或验证码不能为空");
            return "redirect:" + request.getContextPath() + "/login.jsp";
        }
        //判断用户输入不为空                        // 不区分大小写
        if (logincode == null || !logincode.equalsIgnoreCase(sessioncode)) {
            request.getSession().setAttribute("msg", "验证码有误");
            return "redirect:" + request.getContextPath() + "/login.jsp";
        }
        //调用业务层
        Administratorimpl getUser = new Administratorimpl(new userimpl());
        User user = getUser.selectUser(username, password);
        System.out.println("=====uusername====="+user.getUsername());
        //判断密码是否正确
        if (user.getPassword().equals(password)) {
            request.getSession().setAttribute("password",password);
            System.out.println("====password====="+password);
            request.getSession().setAttribute("msg", "密码输入不正确");
            return "redirect:" + request.getContextPath() + "/login.jsp";
        }
        //判断用户名是否正确
        if (!user.getUsername().equals(username)) {
            request.getSession().setAttribute("userName",username);
            request.getSession().setAttribute("msg", "请检查用户名");
            return "redirect:" + request.getContextPath() + "/login.jsp";
        }
        if(!username.equals(user.getUsername())){
            request.getSession().setAttribute("msg","你输入的用户名不存在,请注册后在登录");
            return Constants.FORWARD + request.getContextPath() + "/login.jsp";

        }
        //判断是否勾选自动登录
          if (auto==null){
            //没有勾选   将本地浏览器中的cookie清空
              Cookie cookie = new Cookie(Constants.AUTO_NAME, "");
              //存储到全局路径
              cookie.setPath("/");
              //设置cookie的时间
              cookie.setMaxAge(0);
              //添加
              response.addCookie(cookie);
          }else {
              //自动登录数据库存储 2周
             String content= username+":"+password;
             //将用户名和密码 转码
              content = Base64Utils.encode(content);
              Cookie cookie = new Cookie(Constants.AUTO_NAME, content);
              cookie.setPath("/");
              cookie.setMaxAge(60*60*24*14);
              response.addCookie(cookie);
          }
            if (user != null) {
            request.getSession().setAttribute("loginUser", user);
            return "redirect:" + request.getContextPath() + "/goods?method=index";
        } else {
            return "redirect:" + request.getContextPath() + "/login.jsp";
        }

    }

    //会员注册
    public String register(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
//        /获取页面传入的所有参数   key就是页面的input 的name属性 values 就是 input中的输入的值
           Map<String, String[]> map = request.getParameterMap();
           //将参数封装成user对象
        //创建用户对象
        User user1 = new User();
        //利用beanUtils工具类 将map中的数据 全部自动set到usser对象中
        //使用要求user 对象数学的属性名 必须和页面中的name属性一致 实体类 必须实现 serlizable序列化接口
        BeanUtils.populate(user1,map);
        //调用业务层
        Administratorimpl addone = new Administratorimpl(new userimpl());
        boolean adduser1 = addone.adduser(user1);
        if (adduser1) {
            return Constants.REDIRECT + request.getContextPath() + "/registerSuccess.jsp";
        } else {
            return Constants.REDIRECT + request.getContextPath() + "/register.jsp";
        }
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        String email = request.getParameter("email");
//        String gender = request.getParameter("gender");
//        System.out.println(username + password + email + gender);
//
//        if (username == null || password == null || email == null || gender == null) {
//            return "redirect:" + request.getContextPath() + "/register.jsp";
//        }
//        Administratorimpl administratorimpl = new Administratorimpl(new userimpl());
//        User user = new User(username, password, gender, Constants.NOT_HAS_USER, RandomUtils.createActive(), email, Constants.ACTIVE_FAIL);
//        boolean adduser = administratorimpl.adduser(user);
//        if (adduser) {
//            return Constants.REDIRECT + request.getContextPath() + "/registerSuccess.jsp";
//        } else {
//            return Constants.REDIRECT + request.getContextPath() + "/register.jsp";
//        }
    }
    public String logOut(HttpServletRequest request ,HttpServletResponse response){
        //清空Session 中的数据
        HttpSession session = request.getSession();
        session.removeAttribute("loginUser");
        //清空和覆盖cookie中的值
        Cookie cookie = new Cookie(Constants.AUTO_NAME,"");
        //将保存事件设置为0
        cookie.setMaxAge(0);
        //将cookie的值响应回去
        response.addCookie(cookie);
        //转发到登录页面
        request.getSession().setAttribute("msg","注销成功");
        return "redirect:" + request.getContextPath() + "/login.jsp";
    }
}
